package com.gaj.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring Bean工具类
 * 
 * 职责：
 * 1. 管理Spring ApplicationContext
 * 2. 提供Bean获取功能
 * 3. 提供Bean存在性检查
 * 4. 处理ApplicationContext初始化状态
 * 
 * 使用场景：
 * 在非Spring管理的类中获取Spring容器中的Bean对象
 * 
 * @author 系统
 * @version 1.0
 */
@Component
public class SpringBeanUtils implements ApplicationContextAware {

    //静态变量
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringBeanUtils.applicationContext = applicationContext;
    }

    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static boolean containsBean(String beanName) {
        if (applicationContext == null) {
            System.err.println("Spring ApplicationContext 未初始化");
            return false;
        }
        return applicationContext.containsBean(beanName);
    }
}
